package com.jinlejia.erp.ui.house.prospect;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.prospect.UploadHouseTypeImageAdapter;
import com.jinlejia.erp.adapter.prospect.UploadImageGridViewAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.bean.SaveCookieBean;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.ui.house.prospect.bean.ReasonBean;
import com.jinlejia.erp.ui.house.prospect.bean.ServeyResultBean;
import com.jinlejia.erp.ui.house.prospect.bean.SurveyPictureBean;
import com.jinlejia.erp.utils.ACache;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.DbUtil;
import com.jinlejia.erp.utils.RxUpLoad;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import top.zibin.luban.LubanZ;

/**
 * @author 王进 on 2019/1/10.
 */

public final class Helper {

    private Context context;

    private Helper(Context context) {
        this.context = context;
    }

    public static Helper init(Context context) {
        return new Helper(context);
    }

    //region Util
    private Util mUtil;

    public Util util() {
        if (mUtil == null) {
            mUtil = new Util();
        }
        return mUtil;
    }

    public static class Util implements ReleaseInterface {



        public static class HouseInfo{
            private int roomCount;
            private int drawingCount;
            private int toiletCount;
            private int kitchenCount;

            private HouseInfo(int roomCount, int drawingCount, int toiletCount, int kitchenCount) {
                this.roomCount = roomCount;
                this.drawingCount = drawingCount;
                this.toiletCount = toiletCount;
                this.kitchenCount = kitchenCount;
            }

            public int getRoomCount() {
                return roomCount;
            }

            public int getDrawingCount() {
                return drawingCount;
            }

            public int getToiletCount() {
                return toiletCount;
            }

            public int getKitchenCount() {
                return kitchenCount;
            }

            /**
             * 室-厅-厨-卫
             */
            @Override
            public String toString() {
                String str = "%s-%s-%s-%s";
                return String.format(str,roomCount,drawingCount,kitchenCount,toiletCount);
            }

            public String toText(){
                String str = "%s室%s厅%s厨%s卫";
                return String.format(str,roomCount,drawingCount,kitchenCount,toiletCount);
            }

            public String toSpecialText(){
                String str = "%s室";
                return String.format(str,roomCount);
            }
        }

        private Util() {
            //lock
        }

        int mapUsageNameToUsageType(String name){
            int houseType;
            switch (name) { //根据不同的房产类型，显示不同的Logo
                case "公寓":
                    houseType = Type.USAGE_APARTMENT;
                    break;
                case "写字楼":
                    houseType = Type.USAGE_OFFICE;
                    break;
                case "住宅":
                    houseType = Type.USAGE_RESIDENTIAL;
                    break;
                case "商铺":
                    houseType = Type.USAGE_SHOP;
                    break;
                case "别墅":
                    houseType = Type.USAGE_VILLA;
                    break;
                case "厂房/仓库":
                    houseType = Type.USAGE_WAREHOUSE;
                    break;
                case "当前房屋户型":
                    houseType = Type.USAGE_HOUSE_TYPE;
                    break;
                default:
                    if (name.contains("厂房")){
                        houseType = Type.USAGE_WAREHOUSE;
                    }else {
                        //理论上不会出现这个选项
                        houseType = -1;
                    }
                    break;
            }
            return houseType;
        }


        HouseInfo formatHouseInfo(int roomNum, int drawingNum, int toiletNum, int kitchenNum){
            return new HouseInfo(roomNum,drawingNum,toiletNum,kitchenNum);
        }

        HouseInfo formatHouseInfo(String houseType){
            int roomNum,drawingNum,toiletNum,kitchenNum;
            String[] split = houseType.split("-");
            if (split.length >= 4) {
                roomNum = Integer.parseInt(split[0]);
                drawingNum = Integer.parseInt(split[1]);
                kitchenNum = Integer.parseInt(split[2]);
                toiletNum = Integer.parseInt(split[3]);
            } else {
                return null;
            }
//            if (roomNum==0)return null;
            return new HouseInfo(roomNum,drawingNum,toiletNum,kitchenNum);
        }

        @Override
        public void release() {

        }


    }
    //endregion

    //region Http
    private Http mHttp;

    public Http http() {
        if (mHttp == null) {
            mHttp = new Http(context);
        }
        return mHttp;
    }

    public static class Http implements ReleaseInterface {
        private final Context mContext;
        private ProgressDialog progressDialog;
        private MediaType jsonMediaType;

        private Http(Context context) {
            this.mContext = context;
            progressDialog = new ProgressDialog(mContext);
            jsonMediaType = MediaType.parse("application/json; charset=utf-8");
        }

        //region 查询户型图库

        /**
         * 查询户型图库
         */
        void postListApartment(String rsCode, String houseType, final ListApartmentCallBack callBack) {
            showProgressDialog();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("rsCode", rsCode);
            jsonObject.put("houseType", houseType);
            OkHttpUtils.postString()
                    .url(ApiConfig.PROSPECT_LIST_APARTMENT)
                    .mediaType(jsonMediaType)
                    .content(jsonObject.toString())
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            CustomToast.showToast(mContext, "网络连接错误，请重试", 1500);
                            dismissProgressDialog();
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {
                            if (DataFilter.filter(mContext, response)) {
                                JSONArray data = JSON.parseArray(response.get("data").toString());
                                List<UploadHouseTypeImageAdapter.HouseTypeSource> sources = new ArrayList<>();
                                for (int i = 0; i < data.size(); i++) {
                                    JSONObject json = data.getJSONObject(i);
                                    String houseType = json.getString("houseType"); //eg. 2室1厅1卫
                                    String url = json.getString("url");  //eg. https://img.com/house.jpg
                                    UploadHouseTypeImageAdapter.HouseTypeSource source = new UploadHouseTypeImageAdapter.HouseTypeSource();
                                    source.setTempImageUrl(url);
                                    source.setImageUrl(ApiConfig.MAGEHEADER_URL() +  url);
                                    source.setTypeInfo(houseType);
                                    sources.add(source);
                                }
                                callBack.onResponse(sources);
                            } else {
                                int code = Integer.parseInt(response.get("code").toString());
                                if (code == 501) {
                                    callBack.onEmpty();
                                }
                            }
                            dismissProgressDialog();
                        }
                    });
        }


        public interface ListApartmentCallBack {
            void onResponse(List<UploadHouseTypeImageAdapter.HouseTypeSource> list);

            void onEmpty();
        }
        //endregion

        //region 分间数据字典
        void getCompartmentInfoDictionary(final CompartmentInfoDictionaryCallBack listener) {
            OkHttpUtils.get()
                    .url(ApiConfig.PROSPECT_COMPARTMENT_INFO)
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {
                            if (DataFilter.filter(mContext, response)) {
                                JSONObject data = JSON.parseObject(response.get("data").toString());
                                JSONArray faceInfoList = data.getJSONArray("faceInfoList");
                                JSONArray windowsInfoList = data.getJSONArray("windowsInfoList");
                                List<String> faces = new ArrayList<>();
                                if (faceInfoList == null) {
                                    listener.onFacesResult(null);
                                } else {
                                    for (int i = 0; i < faceInfoList.size(); i++) {
                                        faces.add(faceInfoList.getJSONObject(i).getString("valueName"));
                                    }
                                    listener.onFacesResult(faces);
                                }
                                List<String> windows = new ArrayList<>();
                                if (windowsInfoList == null) {
                                    listener.onWindowsResult(null);
                                } else {
                                    for (int i = 0; i < windowsInfoList.size(); i++) {
                                        windows.add(windowsInfoList.getJSONObject(i).getString("valueName"));
                                    }
                                    listener.onWindowsResult(windows);
                                }
                                listener.onOver();
                            }
                        }
                    });
        }

        public interface CompartmentInfoDictionaryCallBack {
            void onWindowsResult(List<String> windows);

            void onFacesResult(List<String> faces);

            void onOver();
        }
        //endregion

        //region 上传图片

        public synchronized void uploadImage(ArrayList<UploadImageGridViewAdapter.Source> sources, final ArrayList<LubanZ.LubanZImage> files, final UploadImageCallBack callBack) {
            final ArrayList<String> caches = new ArrayList<>(); //成功的缓存文件路径
            final ArrayList<String> failed = new ArrayList<>(); //失败的缓存文件路径
            LubanZ.get(mContext)
                    .load(files)
                    .asObservable()
                    .flatMap(new Func1<LubanZ.LubanZImage, Observable<String>>() {
                        @Override
                        public Observable<String> call(LubanZ.LubanZImage lubanZImage) {
                            Map<String, String> header = new HashMap<>();
                            header.put("token", ACache.get(mContext).getAsString("token") == null ? ""
                                    : ACache.get(mContext).getAsString("token"));
                            header.put("cookie", null == DbUtil.getDB(mContext).single()
                                    .queryById(200, SaveCookieBean.class) ? ""
                                    : DbUtil.getDB(mContext)
                                    .single()
                                    .queryById(200, SaveCookieBean.class).getCookie());
                            File file = lubanZImage.getThumbFile();
                            File originalFile = lubanZImage.getOriginalFile();
                            if (file.length()>=5242880){ //当文件超过或等于5M时，抛出异常
                                throw new IllegalArgumentException("文件过大");
                            }
                            String fileName = URLEncoder.encode(originalFile.getPath());
                            return RxUpLoad.get(mContext)
                                    .url(ApiConfig.IMG_UPLOAD_2)
                                    .file(file)
                                    .fileName(fileName)
                                    .key("uploadFileNew")
                                    .header(header)
                                    .asObservable();
                        }
                    }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<String>() {
                        @Override
                        public synchronized void onCompleted() {
                            if (callBack!=null){
                                callBack.onUploadComplete(failed);
                            }
                        }

                        @Override
                        public synchronized void onError(Throwable e) {
                            e.printStackTrace();
                            if (e instanceof IOException){
                                CustomToast.showToast(mContext,"网络连接失败",1500);
                            }else {
                                CustomToast.showToast(mContext,e.getMessage(),1500);
                            }

                            if (callBack != null) {
                                for (LubanZ.LubanZImage f:files) {
                                    String path = f.getOriginalFile().getPath();
                                    String encode = URLEncoder.encode(path);
                                    if (!caches.contains(encode)){
                                        failed.add(encode);
                                    }

                                }
                                callBack.onUploadComplete(failed);
                            }
                        }

                        @Override
                        public synchronized void onNext(String s) {
                            JSONObject jsonObject = JSON.parseObject(s);
                            JSONObject data = jsonObject.getJSONObject("data");
                            String fileName = data.getString("fileName");
                            if (jsonObject.get("code").toString().equals("200")) {
                                String url = data.getString("url");
                                if (callBack != null) {
                                    callBack.onUploadSuccess(fileName, url);
                                }
                                caches.add(url);
                            } else {
                                failed.add(fileName);
                            }
                        }
                    })

            ;
        }

        public interface UploadImageCallBack {
            void onUploadSuccess(String fileName, String serverUrl);

            void onUploadComplete(ArrayList<String> failedList);
        }
        //endregion

        //region 上传实勘图片
        public void saveSurveyPicture(String type, String applyCode,SurveyPictureBean bean, final SaveSurveyPictureCallBack callBack) {
            String url;
            if ("shs".equals(type)) {
                url = ApiConfig.PROSPECT_SHS_SAVE;
            } else {
                url = ApiConfig.PROSPECT_RENT_SAVE;
            }
            if (applyCode==null){
                applyCode = "";
            }
            bean.setApplyCode(applyCode);
            String jsonString = JSON.toJSONString(bean);
            System.out.println("上传房屋信息::"+jsonString);
            OkHttpUtils.postString()
                    .url(url)
                    .mediaType(jsonMediaType)
                    .content(jsonString)
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            CustomToast.showToast(mContext,"网络连接失败",1500);
                            callBack.onResult(false);
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {
                            if (DataFilter.filter(mContext,response)){
                                if (callBack!=null){
                                    callBack.onResult(true);
                                }
                            }else {
                                if (callBack!=null){
                                    callBack.onResult(false);
                                }
                            }
                        }
                    });
        }

        public interface SaveSurveyPictureCallBack{
            void onResult(boolean result);
        }
        //endregion

        //region 顶替理由数字字典
        public void prospectReason(final ProspectReasonCallBack callBack,final ApartmentCallBack apartmentCallBack) {
            OkHttpUtils.get()
                    .url(ApiConfig.PROSPECT_REASON)
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            CustomToast.showToast(mContext,"网络连接失败",1500);
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {
                            if (DataFilter.filter(mContext, response)) {
                                JSONObject data = JSON.parseObject(response.get("data").toString());
                                ArrayList<Map<String,Object>> residenceS = JSON.parseObject(data.getJSONArray("zhuzhai-shi").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> residenceT = JSON.parseObject(data.getJSONArray("zhuzhai-ting").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> residenceW = JSON.parseObject(data.getJSONArray("zhuzhai-wei").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> residenceC = JSON.parseObject(data.getJSONArray("zhuzhai-chu").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });

                                ArrayList<Map<String,Object>> apartmentS = JSON.parseObject(data.getJSONArray("gongyu-shi").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> apartmentT = JSON.parseObject(data.getJSONArray("gongyu-ting").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> apartmentW = JSON.parseObject(data.getJSONArray("gongyu-wei").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> apartmentC = JSON.parseObject(data.getJSONArray("gongyu-chu").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });

                                ArrayList<Map<String,Object>> villaS = JSON.parseObject(data.getJSONArray("bieshu-shi").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> villaT = JSON.parseObject(data.getJSONArray("bieshu-ting").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> villaW = JSON.parseObject(data.getJSONArray("bieshu-wei").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                ArrayList<Map<String,Object>> villaC = JSON.parseObject(data.getJSONArray("bieshu-chu").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                apartmentCallBack.onShowApartmentList(residenceS,residenceT,residenceW,residenceC,apartmentS,apartmentT,apartmentW,apartmentC,villaS,villaT,villaW,villaC);
                                JSONArray array = data.getJSONArray("substitutionList");
                                List<ReasonBean> reasonBeans = null;
                                try{
                                    reasonBeans = JSON.parseArray(array.toString(), ReasonBean.class);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                                if (callBack != null) {
                                    if (reasonBeans == null) {
                                        callBack.onShowReasonList(null);
                                    } else {
                                        callBack.onShowReasonList(reasonBeans);
                                    }
                                }
                            }
                        }
                    });
        }

        public interface ProspectReasonCallBack {
            void onShowReasonList(List<ReasonBean> beanList);
        }

        public interface ApartmentCallBack {
            void onShowApartmentList(ArrayList<Map<String, Object>> residenceS, ArrayList<Map<String, Object>> residenceT, ArrayList<Map<String, Object>> residenceW, ArrayList<Map<String, Object>> residenceC, ArrayList<Map<String, Object>> apartmentS, ArrayList<Map<String, Object>> apartmentT, ArrayList<Map<String, Object>> apartmentW, ArrayList<Map<String, Object>> apartmentC, ArrayList<Map<String, Object>> villaS, ArrayList<Map<String, Object>> villaT, ArrayList<Map<String, Object>> villaW, ArrayList<Map<String, Object>> villaC);
        }
        //endregion

        //region 查询实勘详情
        public void getServey(String type, final String rsCode, final GetServeyCallBack callBack) {

            String url;
            if ("shs".equals(type)) {
                url = ApiConfig.PROSPECT_SHS_SERVEY;
            } else {
                url = ApiConfig.PROSPECT_RENT_SERVEY;
            }
            OkHttpUtils.get()
                    .url(url.concat(rsCode))
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            CustomToast.showToast(mContext,"网络连接失败",1500);
                            if (callBack != null) {
                                callBack.showServeyResult(null);
                            }
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {
                            if (DataFilter.filter(mContext, response)) {
                                JSONObject data = JSON.parseObject(response.get("data").toString());
                                ServeyResultBean bean = null;
                                try {
                                    bean = JSON.parseObject(data.toString(),ServeyResultBean.class);
                                    bean.setRsCode(rsCode);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                if (callBack != null) {
                                    callBack.showServeyResult(bean);
                                }
                            }
                        }
                    });
        }

        public interface GetServeyCallBack {
            void showServeyResult(ServeyResultBean bean);
        }
        //endregion

        //region 补充户型图
        void saveApartmentPicture(String type, String apartmentPicturnUrl, String rscode, final SaveApartmentPictureCallBack callBack){
            String url;
            if ("shs".equals(type)){
                url = ApiConfig.HOUSE_SOURCE_SHS_APARTMENT;
            }else {
                url = ApiConfig.HOUSE_SOURCE_RENT_APARTMENT;
            }
            JSONObject json = new JSONObject();
            json.put("apartmentPicturnUrl",apartmentPicturnUrl);
            json.put("rscode",rscode);
            System.out.println("补充户型图："+json.toString());
            OkHttpUtils.postString()
                    .url(url)
                    .mediaType(jsonMediaType)
                    .content(json.toString())
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            CustomToast.showToast(mContext,"网络错误",1500);
                            if (callBack!=null){
                                callBack.onFailed();
                            }
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {
                            if (DataFilter.filter(mContext,response)){
                                if (callBack!=null){
                                    callBack.onSuccess();
                                }
                            }else {
                                if (callBack!=null){
                                    callBack.onFailed();
                                }
                            }
                        }
                    });
        }

        public interface SaveApartmentPictureCallBack{
            void onSuccess();
            void onFailed();
        }
        //endregion

        //region 分间信息
        public void listRoomDivision(String rsCode, final String houseName, final ListRoomDivisionCallBack callBack){
            OkHttpUtils.get()
                    .url(ApiConfig.PROSPECT_LIST_ROOM_DIVISION.concat(rsCode))
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
//                            CustomToast.showToast(mContext,"网络连接失败",1500);
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {
                            JSONArray data = null;
                            try{
                                int code = Integer.parseInt(response.get("code").toString());
                                data = JSON.parseArray(String.valueOf(response.get("data")));
                            }catch (Exception ignored){
                            }
                             if (data==null)return;
                            for (int i = 0; i < data.size(); i++) {
                                JSONObject object = data.getJSONObject(i);
                                if (TextUtils.equals(houseName, object.getString("name"))) {
                                    String face = object.getString("face");
                                    String windows = object.getString("windows");
                                    String name = object.getString("name");
                                    String square = object.getString("square");
                                    if (callBack != null) {
                                        callBack.onFacesResult(face);
                                        callBack.onWindowsResult(windows);
                                        callBack.onNameResult(name);
                                        callBack.onSquareResult(square);
                                    }
                                    break;
                                }
                            }
                        }
                    });
        }

        public interface ListRoomDivisionCallBack{
            void onWindowsResult(String windows);
            void onFacesResult(String faces);
            void onNameResult(String name);
            void onSquareResult(String square);
        }
        //endregion

        //region 清空实勘图片
        @Deprecated
        public void delServey(String type,String applyCode){
            String url ;
            if ("shs".equals(type)){
                url = ApiConfig.HOUSE_SOURCE_SHS_DEL_SERVEY;
            }else {
                url = ApiConfig.HOUSE_SOURCE_RENT_DEL_SERVEY;
            }
            JSONObject json = new JSONObject();
            json.put("applyCode",applyCode);
            OkHttpUtils.postString()
                    .url(url)
                    .content(json.toString())
                    .mediaType(jsonMediaType)
                    .build()
                    .execute(new MapCallBack() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            e.printStackTrace();
                            CustomToast.showToast(mContext,"删除失败,网络错误",1500);
                        }

                        @Override
                        public void onResponse(Map<String, Object> response, int id) {

                        }
                    });
        }
        //endregion

        private void showProgressDialog(){
            if (progressDialog==null){
                progressDialog = new ProgressDialog(mContext);
            }
            progressDialog.show();
        }

        private void dismissProgressDialog(){
            if (progressDialog != null){
                progressDialog.dismiss();
            }
        }

        @Override
        public void release() {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
            progressDialog = null;
        }
    }
    //endregion

    //region Message
    private Message mMessage;

    public Message message() {
        if (mMessage == null) {
            if (!(context instanceof Activity)) {
                throw new IllegalArgumentException();
            }
            mMessage = new Message((Activity) context);
        }
        return mMessage;
    }

    public static class Message implements ReleaseInterface {
        private RelativeLayout tipMessageLayout; //总的提示控件
        private View topMessageBgView; //顶部背景色
        private View centerMessageBgView; //中间的背景色
        private View bottomMessageBgView; //底部的背景色
        private LinearLayout warnMessageLayout; //驳回警告Layout
        private TextView errorMessageTextView; //驳回的警告信息
        private LinearLayout tipHouseInfoLayout; //提示房屋信息Layout
        private ImageView tipHouseImageView; //提示房屋的图片，根据住宅
        private TextView tipHouseTextView; //提示访问的文本
        private LinearLayout tipMessageBgLayout;

        private Message(Activity activity) {
            tipMessageLayout = activity.findViewById(R.id.tipMessageLayout);
            topMessageBgView = activity.findViewById(R.id.topMessageBgView);
            centerMessageBgView = activity.findViewById(R.id.centerMessageBgView);
            bottomMessageBgView = activity.findViewById(R.id.bottomMessageBgView);
            warnMessageLayout = activity.findViewById(R.id.warnMessageLayout);
            errorMessageTextView = activity.findViewById(R.id.errorMessageTextView);
            tipHouseInfoLayout = activity.findViewById(R.id.tipHouseInfoLayout);
            tipHouseImageView = activity.findViewById(R.id.tipHouseImageView);
            tipHouseTextView = activity.findViewById(R.id.tipHouseTextView);
            tipMessageBgLayout = activity.findViewById(R.id.tipMessageBgLayout);
        }

        private boolean showErrorMessage = false;

        public void showErrorMessage(String msg) {
            showErrorMessage = true;
            initBg();
            innerShowErrorMessage(msg);
        }

        private void innerShowErrorMessage(String msg) {
            warnMessageLayout.setVisibility(View.VISIBLE);
            errorMessageTextView.setVisibility(View.VISIBLE);
            errorMessageTextView.setText(String.format("驳回原因：%s", msg));
        }

        private boolean showHouseMessage = false;

        public void showHouseInfo(int type){
            showHouseMessage = true;
            initBg();
            innerShowHouseInfo(type, -1, -1, -1, -1);
        }
        public boolean hasHouseType(int type){
            boolean has;
            switch (type) { //根据不同的房产类型，显示不同的Logo
                case Type.USAGE_APARTMENT:
                    has = true;
                    break;
                case Type.USAGE_VILLA:
                    has = true;
                    break;
                case Type.USAGE_RESIDENTIAL:
                    has = true;
                    break;
                case Type.USAGE_OFFICE:
                case Type.USAGE_SHOP:
                case Type.USAGE_WAREHOUSE:
                case Type.USAGE_HOUSE_TYPE:
                    has = false;
                    break;
                default:
                    //理论上不会出现这个选项
                    has = false;
                    break;
            }
            return has;
        }

        public void showHouseInfo(int type, Util.HouseInfo info){
            if (info==null){
                showHouseInfo(type);
                return;
            }
            showHouseMessage = true;
            initBg();
            innerShowHouseInfo(type, info.getRoomCount(), info.getDrawingCount(), info.getToiletCount(), info.getKitchenCount());
        }

        String getUsageNameByUsageType(int type) {
            String houseType;
            switch (type) { //根据不同的房产类型，显示不同的Logo
                case Type.USAGE_APARTMENT:
                    houseType = "公寓";
                    break;
                case Type.USAGE_OFFICE:
                    houseType = "写字楼";
                    break;
                case Type.USAGE_RESIDENTIAL:
                    houseType = "住宅";
                    break;
                case Type.USAGE_SHOP:
                    houseType = "商铺";
                    break;
                case Type.USAGE_VILLA:
                    houseType = "别墅";
                    break;
                case Type.USAGE_WAREHOUSE:
                    houseType = "厂房/仓库";
                    break;
                case Type.USAGE_HOUSE_TYPE:
                    houseType = "当前房屋户型";
                    break;
                default:
                    //理论上不会出现这个选项
                    houseType = "未知";
                    break;
            }
            return houseType;
        }

        private void innerShowHouseInfo(int type, int roomNum, int drawingRoomNum, int toiletNum, int kitchenNum) {
            Util.HouseInfo info = new Util.HouseInfo(roomNum,drawingRoomNum,toiletNum,kitchenNum);
            String houseType;
            String houseInfo = "%s：%s";
            switch (type) { //根据不同的房产类型，显示不同的Logo
                case Type.USAGE_APARTMENT:
                    houseType = "公寓";
                    break;
                case Type.USAGE_OFFICE:
                    houseType = "写字楼";
                    break;
                case Type.USAGE_RESIDENTIAL:
                    houseType = "住宅";
                    break;
                case Type.USAGE_SHOP:
                    houseType = "商铺";
                    break;
                case Type.USAGE_VILLA:
                    houseType = "别墅";
                    break;
                case Type.USAGE_WAREHOUSE:
                    houseType = "厂房/仓库";
                    break;
                case Type.USAGE_HOUSE_TYPE:
                    houseType = "当前房屋户型";
                    break;
                default:
                    houseType = "";
                    break;
            }

            tipHouseInfoLayout.setVisibility(View.VISIBLE);
            tipHouseImageView.setVisibility(View.VISIBLE);
            tipHouseTextView.setVisibility(View.VISIBLE);
            if (roomNum==-1 && drawingRoomNum==-1 && toiletNum ==-1 && kitchenNum == -1){
                houseInfo = houseType;
            }else {
                houseInfo = String.format(Locale.CHINESE, houseInfo,houseType,info.toText());
            }
            tipHouseTextView.setText(houseInfo);
        }


        //isAll-> true:显示两个提示信息 false：只显示一个提示信息
        private void initBg() {
            boolean isAll = showErrorMessage && showHouseMessage;
            tipMessageLayout.setVisibility(View.VISIBLE);
            if (isAll) {
                topMessageBgView.setVisibility(View.VISIBLE);
                bottomMessageBgView.setVisibility(View.VISIBLE);
            } else {
                topMessageBgView.setVisibility(View.GONE);
                bottomMessageBgView.setVisibility(View.GONE);
            }
            tipMessageBgLayout.setVisibility(View.VISIBLE);
            centerMessageBgView.setVisibility(View.VISIBLE);

        }

        public void closeMessage() {
            showHouseMessage = false;
            showErrorMessage = false;
            tipMessageLayout.setVisibility(View.GONE);
            topMessageBgView.setVisibility(View.GONE);
            tipMessageBgLayout.setVisibility(View.GONE);
            centerMessageBgView.setVisibility(View.GONE);
            bottomMessageBgView.setVisibility(View.GONE);
            tipHouseInfoLayout.setVisibility(View.GONE);
            tipHouseImageView.setVisibility(View.GONE);
            tipHouseTextView.setVisibility(View.GONE);
            warnMessageLayout.setVisibility(View.GONE);
            errorMessageTextView.setVisibility(View.GONE);
        }

        @Override
        public void release() {
            //empty 信息展示不需要进行释放
        }
    }
    //endregion

    public void release() {
        if (mUtil != null) {
            mUtil.release();
        }
        mUtil = null;
        if (mHttp != null) {
            mHttp.release();
        }
        mHttp = null;
        if (mMessage != null) {
            mMessage.release();
        }
        mMessage = null;
    }

    private interface ReleaseInterface {
        void release();
    }
}
